package com.example.sqlite2.app;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity {

    public String barData, barFormat;
    SQLiteDatabase db;
    TableRow tableRow;
    TextView textView,textView1,textView2,textView4;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*The two lines after the comments are to create the sqlite database on the phone.
          Several different ways, but seems that openOrCreateDatabase it the common type
          Note that this can only be accessed from inside the program on the phone. Doesn't
          even look like it can be found on the phone through search or the like unless
          the phone is rooted. Also, if you want to change the database around, you HAVE
          to clear the app's data, and possibly then remove the app entirely before you
          rerun it, or it will crash, because it will try to use the old version of the
          databse
        */
        db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS BCScan(bdata VARCHAR,bformat VARCHAR);");

        try {
            Button scanner = (Button)findViewById(R.id.scanner);
            scanner.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0);
                }

            });

        } catch (ActivityNotFoundException anfe) {
            Log.e("onCreate", "Scanner Not Found", anfe);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

                barData = intent.getStringExtra("SCAN_RESULT");
                barFormat = intent.getStringExtra("SCAN_RESULT_FORMAT");
               /*
                Thread sendThread = new Thread() {
                    public void run() {
                        sendBarcode(barData);
                    }
                };
                sendThread.start();
                */

                //how to handle the insert into the sqlite database
                db.execSQL("INSERT INTO BCScan VALUES('"+barData+"','"+barFormat+"');");

                Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format , Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400); //here it is (context, text, duration)
                toast.show();

            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
                Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400); //here it is (context, text, duration)
                toast.show();

            }

        }


}

    /*
    the display function to show what's in the database
     */
    public void showdata(View view)
    {
        //a side note - you don't have to use c as the var here
        Cursor c=db.rawQuery("SELECT * from BCScan", null);
        int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
        TableRow tableRow;
        //doesn't have to skip from textView2 to 4 here. Was just due to
        //my own mistakes in previous tries and I've left it that way
        TextView textView,textView1,textView2,textView4;
        tableRow = new TableRow(getApplicationContext());
        textView=new TextView(getApplicationContext());
        textView.setText("Data");
        textView.setTextColor(Color.RED);
        textView.setTypeface(null, Typeface.BOLD);
        textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);
        textView4=new TextView(getApplicationContext());
        textView4.setText("Format");
        textView4.setTextColor(Color.RED);
        textView4.setTypeface(null, Typeface.BOLD);
        textView4.setPadding(20, 20, 20, 20);
        tableRow.addView(textView4);
        tableLayout.addView(tableRow);
        for (Integer j = 0; j < count; j++)
        {
            tableRow = new TableRow(getApplicationContext());
            textView1 = new TextView(getApplicationContext());
            textView1.setText(c.getString(c.getColumnIndex("bdata")));
            textView2 = new TextView(getApplicationContext());
            textView2.setText(c.getString(c.getColumnIndex("bformat")));
            textView1.setPadding(20, 20, 20, 20);
            textView2.setPadding(20, 20, 20, 20);
            tableRow.addView(textView1);
            tableRow.addView(textView2);
            tableLayout.addView(tableRow);
            c.moveToNext() ;
        }
        setContentView(tableLayout);
        db.close();
    }
    public void close(View view)
    {
        System.exit(0);
    }
}
